package gui.AddCoordinateDialog;

import gui.GraphicVariables;
import gui.SetupPanel;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import logic.objects.Problem;

public class AddCoordinateFrame extends JFrame implements ActionListener {
	JButton add;
	JButton cancle;
	JTextField x;
	JTextField y;
	JLabel xLable;
	JLabel yLable;
	SetupPanel setupPanel;

	public AddCoordinateFrame(SetupPanel setupPanel) {
		JPanel panel = new JPanel(new GridLayout(3, 2));
		this.setSize(200, 100);
		this.setLocation(
				setupPanel.getLocationOnScreen().x + setupPanel.getWidth() / 2
						- this.getWidth() / 2,
				setupPanel.getLocationOnScreen().y + setupPanel.getHeight() / 2
						- this.getHeight() / 2);

		add(panel);
		xLable = new JLabel("x");
		yLable = new JLabel("y");
		x = new JTextField();
		y = new JTextField();
		add = new JButton("Hinzufügen");
		cancle = new JButton("Abbrechen");
		add.addActionListener(this);
		cancle.addActionListener(this);
		panel.add(xLable);
		panel.add(yLable);
		panel.add(x);
		panel.add(y);
		panel.add(add);
		panel.add(cancle);
		setVisible(true);
		this.setupPanel = setupPanel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == add) {
			try {
				int xZahl = Integer.parseInt(x.getText());
				int yZahl = Integer.parseInt(y.getText());

				if (xZahl < 0 || yZahl < 0 || xZahl > 1000 || yZahl > 1000) {
					JOptionPane.showMessageDialog(this,
							"Bitte tragen Sie Zahlen zwischen 0 und 1000 ein");
				} else {
					if (setupPanel.check(xZahl, xZahl)) {
						setupPanel.add(xZahl, xZahl);
						this.setVisible(false);
					} else {
						JOptionPane.showMessageDialog(this,
								"Die Koordinaten sind bereits eingetragen");
					}
				}
			} catch (NumberFormatException ex) {
				JOptionPane.showMessageDialog(this,
						"Bitte tragen Sie ganze Zahlen in die Felder ein");

			}

		} else if (e.getSource() == cancle) {
			this.setVisible(false);
		}
	}

}
